import eventBus from "./eventBus";
import socket from "./socket";
interface Msg {
  type: String;
  data: any;
}
class androidMessage {
  public music_lists: any = [];
  constructor() {
    this.init();
    this.send_msg({
      type: "get_music",
      data: ""
    });
  }

  public init() {
    let self = this;
    (window as any).revice_msg = function(param: any) {
      console.log("recive messages forme android:", param);
      try {
        let data = JSON.parse(param.data);
        if (param.type == "get_music") {
          self.music_lists = data;
          console.log("解析安卓data:", data);
        } else if (param.type == "request_music") {
          socket.sendMsg("message", {
            type: "fileUrl",
            data: data
          });
        }
      } catch (e) {
        console.log("解析错误:", e);
      }
    };
  }

  public send_msg(param: Msg) {
    console.log("send android message");

    try {
      if (param.type == "get_music") {
      }
      (window as any).sharemusic.recive_msg(JSON.stringify(param));
    } catch (e) {
      console.log("发送消息失败");
    }
  }
}

export default new androidMessage();
